package com.zw.yzk.kabu.skin;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;
import skin.support.content.res.SkinCompatResources;
import skin.support.widget.SkinCompatTextHelper;

/**
 * Created by skydar on 2017/4/18.
 */

public class SkinCompatCheckBoxAttrHelper extends SkinCompatTextHelper {

    public static SkinCompatCheckBoxAttrHelper create(TextView view) {
        return new SkinCompatCheckBoxAttrHelper(view);
    }


    public SkinCompatCheckBoxAttrHelper(TextView view) {
        super(view);
        mView = view;
        context = view.getContext();
    }

    final TextView mView;
    final Context context;
    private int mTextColorResId = INVALID_ID;

    public ColorStateList getDrawableColor(int resId) {
        ColorStateList colorStateList = ContextCompat.getColorStateList(context, resId);
        if ( SkinCompatResources.getInstance().isDefaultSkin()) {
            return colorStateList;
        }

        String resName = context.getResources().getResourceEntryName(resId);

        int targetResId = SkinCompatResources.getInstance().getSkinResources().getIdentifier(resName, "drawable", SkinCompatResources.getInstance().getSkinPkgName());

        return targetResId == 0 ? colorStateList : SkinCompatResources.getInstance().getSkinResources().getColorStateList(targetResId);
    }

    private void applyTextColorResource() {
        mTextColorResId = checkResourceId(mTextColorResId);
        if (mTextColorResId == skin.support.R.color.abc_primary_text_disable_only_material_light
                || mTextColorResId == skin.support.R.color.abc_secondary_text_material_light) {
            return;
        }
        if (mTextColorResId != INVALID_ID) {
            String typeName = mView.getResources().getResourceTypeName(mTextColorResId);
            if ("color".equals(typeName)) {
                ColorStateList color = SkinCompatResources.getInstance().getColorStateList(mTextColorResId);
                mView.setTextColor(color);
            } else if ("drawable".equals(typeName)) {
                ColorStateList color = getDrawableColor(mTextColorResId);
                mView.setTextColor(color);
            }
        }
    }

    public void applySkin() {
        mTextColorResId = getTextColorResId();

        applyCompoundDrawablesResource();

        applyTextColorResource();
    }
}
